{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f548527b",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np  # 导入NumPy库 "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cc7a97f3",
   "metadata": {},
   "source": [
    "# 2.1.1创建数组对象"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "62633b0d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " 创建的数组为：  [1 2 3 4]\n"
     ]
    }
   ],
   "source": [
    "arr1 = np.array([1, 2, 3, 4])  # 创建一维数组 \n",
    "print(' 创建的数组为： ', arr1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f05009b4",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "a178c8e0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "numpy.ndarray"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(arr1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "0285c28c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "创建的数组为：\n",
      " [[ 1  2  3  4]\n",
      " [ 4  5  6  7]\n",
      " [ 7  8  9 10]]\n"
     ]
    }
   ],
   "source": [
    "# 创建二维数组\n",
    "arr2 = np.array([\n",
    "    [1, 2, 3, 4],\n",
    "    [4, 5, 6, 7],\n",
    "    [7, 8, 9, 10]\n",
    "])\n",
    "print('创建的数组为：\\n', arr2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "b88e5c5a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "数组维度为： 2\n"
     ]
    }
   ],
   "source": [
    "print('数组维度为：', arr2.ndim)# 查看数组维度"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "626937de",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "数组维度为： (3, 4)\n"
     ]
    }
   ],
   "source": [
    "print('数组维度为：', arr2.shape)# 查看数组结构"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "c3a51fb2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "数组类型为： int32\n"
     ]
    }
   ],
   "source": [
    "print('数组类型为：', arr2.dtype)  # 查看数组类型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "da182f19",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "数组元素个数为： 12\n"
     ]
    }
   ],
   "source": [
    "print('数组元素个数为：', arr2.size)  # 查看数组元素个数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "e86e5785",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "数组每个元素大小为： 4\n"
     ]
    }
   ],
   "source": [
    "print('数组每个元素大小为：', arr2.itemsize)  # 查看数组每个元素大小"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "c07fc003",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "重新设置shape 后的arr2 为：\n",
      " [[ 1  2  3]\n",
      " [ 4  4  5]\n",
      " [ 6  7  7]\n",
      " [ 8  9 10]]\n"
     ]
    }
   ],
   "source": [
    "arr2.shape = 4,3  # 重新设置shape\n",
    "print('重新设置shape 后的arr2 为：\\n', arr2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "7ca11a0e",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "使用arange函数创建的数组为：\n",
      " [0.  0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9]\n"
     ]
    }
   ],
   "source": [
    "# 代码 2-3\n",
    "print('使用arange函数创建的数组为：\\n', np.arange(0,1,0.1))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "8b47a174",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "使用arange函数创建的数组为：\n",
      " [  1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17  18\n",
      "  19  20  21  22  23  24  25  26  27  28  29  30  31  32  33  34  35  36\n",
      "  37  38  39  40  41  42  43  44  45  46  47  48  49  50  51  52  53  54\n",
      "  55  56  57  58  59  60  61  62  63  64  65  66  67  68  69  70  71  72\n",
      "  73  74  75  76  77  78  79  80  81  82  83  84  85  86  87  88  89  90\n",
      "  91  92  93  94  95  96  97  98  99 100]\n"
     ]
    }
   ],
   "source": [
    "# 代码 我想生成1到100的数列怎么写\n",
    "print('使用arange函数创建的数组为：\\n', np.arange(1,101,1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "f145d0e2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "使用linspace函数创建的数组为：\n",
      " [0.         0.09090909 0.18181818 0.27272727 0.36363636 0.45454545\n",
      " 0.54545455 0.63636364 0.72727273 0.81818182 0.90909091 1.        ]\n"
     ]
    }
   ],
   "source": [
    "# 代码 2-4\n",
    "print('使用linspace函数创建的数组为：\\n', np.linspace(0, 1, 12))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "242d6c37",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "使用linspace函数创建的数组为：\n",
      " [ 0.  1.  2.  3.  4.  5.  6.  7.  8.  9. 10.]\n"
     ]
    }
   ],
   "source": [
    "# 代码 2-4 生成0到10，一共多少元素？\n",
    "print('使用linspace函数创建的数组为：\\n', np.linspace(0, 10, 11))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "155d32b6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "使用logspace函数创建的数组为：\n",
      " [  1.           1.27427499   1.62377674   2.06913808   2.6366509\n",
      "   3.35981829   4.2813324    5.45559478   6.95192796   8.8586679\n",
      "  11.28837892  14.38449888  18.32980711  23.35721469  29.76351442\n",
      "  37.92690191  48.32930239  61.58482111  78.47599704 100.        ]\n"
     ]
    }
   ],
   "source": [
    "# 代码 2-5\n",
    "print('使用logspace函数创建的数组为：\\n', np.logspace(0, 2, 20))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "31830a51",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "使用logspace函数创建的数组为：\n",
      " [  10.  100. 1000.]\n"
     ]
    }
   ],
   "source": [
    "# 代码 2-5 要生成10到10万的数列，每隔10倍\n",
    "print('使用logspace函数创建的数组为：\\n', np.logspace(1, 3, 3))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "5ddd312d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "使用zeros函数创建的数组为：\n",
      " [0. 0. 0.]\n"
     ]
    }
   ],
   "source": [
    "# 代码 2-6\n",
    "print('使用zeros函数创建的数组为：\\n', np.zeros(3))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "066b8fc4",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "使用eye函数创建的数组为：\n",
      " [[1. 0. 0. 0. 0. 0. 0. 0.]\n",
      " [0. 1. 0. 0. 0. 0. 0. 0.]\n",
      " [0. 0. 1. 0. 0. 0. 0. 0.]\n",
      " [0. 0. 0. 1. 0. 0. 0. 0.]\n",
      " [0. 0. 0. 0. 1. 0. 0. 0.]\n",
      " [0. 0. 0. 0. 0. 1. 0. 0.]\n",
      " [0. 0. 0. 0. 0. 0. 1. 0.]\n",
      " [0. 0. 0. 0. 0. 0. 0. 1.]]\n"
     ]
    }
   ],
   "source": [
    "# 代码 2-7\n",
    "print('使用eye函数创建的数组为：\\n', np.eye(8))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "id": "90e9bc47",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\42082\\AppData\\Local\\Temp\\ipykernel_21280\\2296144808.py:3: FutureWarning: In the future `np.bool` will be defined as the corresponding NumPy scalar.\n",
      "  print('转换结果为：', np.bool(42))  # 整型转换为布尔型\n"
     ]
    },
    {
     "ename": "AttributeError",
     "evalue": "module 'numpy' has no attribute 'bool'.\n`np.bool` was a deprecated alias for the builtin `bool`. To avoid this error in existing code, use `bool` by itself. Doing this will not modify any behavior and is safe. If you specifically wanted the numpy scalar type, use `np.bool_` here.\nThe aliases was originally deprecated in NumPy 1.20; for more details and guidance see the original release note at:\n    https://numpy.org/devdocs/release/1.20.0-notes.html#deprecations",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mAttributeError\u001b[0m                            Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[74], line 3\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[38;5;66;03m# 代码 2-10\u001b[39;00m\n\u001b[1;32m----> 3\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m转换结果为：\u001b[39m\u001b[38;5;124m'\u001b[39m, np\u001b[38;5;241m.\u001b[39mbool(\u001b[38;5;241m42\u001b[39m))\n",
      "File \u001b[1;32mD:\\03env\\anaconda3-2023.7\\Lib\\site-packages\\numpy\\__init__.py:305\u001b[0m, in \u001b[0;36m__getattr__\u001b[1;34m(attr)\u001b[0m\n\u001b[0;32m    300\u001b[0m     warnings\u001b[38;5;241m.\u001b[39mwarn(\n\u001b[0;32m    301\u001b[0m         \u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mIn the future `np.\u001b[39m\u001b[38;5;132;01m{\u001b[39;00mattr\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m` will be defined as the \u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[0;32m    302\u001b[0m         \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mcorresponding NumPy scalar.\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;167;01mFutureWarning\u001b[39;00m, stacklevel\u001b[38;5;241m=\u001b[39m\u001b[38;5;241m2\u001b[39m)\n\u001b[0;32m    304\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m attr \u001b[38;5;129;01min\u001b[39;00m __former_attrs__:\n\u001b[1;32m--> 305\u001b[0m     \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mAttributeError\u001b[39;00m(__former_attrs__[attr])\n\u001b[0;32m    307\u001b[0m \u001b[38;5;66;03m# Importing Tester requires importing all of UnitTest which is not a\u001b[39;00m\n\u001b[0;32m    308\u001b[0m \u001b[38;5;66;03m# cheap import Since it is mainly used in test suits, we lazy import it\u001b[39;00m\n\u001b[0;32m    309\u001b[0m \u001b[38;5;66;03m# here to save on the order of 10 ms of import time for most users\u001b[39;00m\n\u001b[0;32m    310\u001b[0m \u001b[38;5;66;03m#\u001b[39;00m\n\u001b[0;32m    311\u001b[0m \u001b[38;5;66;03m# The previous way Tester was imported also had a side effect of adding\u001b[39;00m\n\u001b[0;32m    312\u001b[0m \u001b[38;5;66;03m# the full `numpy.testing` namespace\u001b[39;00m\n\u001b[0;32m    313\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m attr \u001b[38;5;241m==\u001b[39m \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mtesting\u001b[39m\u001b[38;5;124m'\u001b[39m:\n",
      "\u001b[1;31mAttributeError\u001b[0m: module 'numpy' has no attribute 'bool'.\n`np.bool` was a deprecated alias for the builtin `bool`. To avoid this error in existing code, use `bool` by itself. Doing this will not modify any behavior and is safe. If you specifically wanted the numpy scalar type, use `np.bool_` here.\nThe aliases was originally deprecated in NumPy 1.20; for more details and guidance see the original release note at:\n    https://numpy.org/devdocs/release/1.20.0-notes.html#deprecations"
     ]
    }
   ],
   "source": [
    "# 代码 2-10\n",
    "\n",
    "print('转换结果为：', np.bool(42))  # 整型转换为布尔型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "3558281f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "转换结果为： True\n"
     ]
    }
   ],
   "source": [
    "# 代码 2-10\n",
    "\n",
    "print('转换结果为：', np.bool_(-42))  # 整型转换为布尔型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "ddd21452",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "转换结果为： False\n"
     ]
    }
   ],
   "source": [
    "print('转换结果为：', bool(0))  # 整型转换为布尔型"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "af046b05",
   "metadata": {},
   "source": [
    "# 2.1.2生成随机数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "ab9ba8ba",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "生成的随机数组为：\n",
      " [0.38204138 0.22690406 0.68663711 0.08193469 0.9191955  0.98542677\n",
      " 0.11580525 0.34289675 0.62856357 0.44974171 0.11126521 0.95645197\n",
      " 0.1103073  0.6241357  0.13187966 0.81347008 0.93496844 0.62119953\n",
      " 0.80658732 0.85215205 0.50523912 0.89735575 0.39961504 0.18591813\n",
      " 0.0525936  0.6348666  0.6203013  0.23982176 0.83153392 0.73465561\n",
      " 0.21267649 0.33709061 0.55784784 0.52092373 0.10692939 0.86163626\n",
      " 0.92769717 0.47740279 0.39108116 0.82739496 0.55799173 0.40601503\n",
      " 0.74860204 0.364536   0.78455165 0.16807307 0.6331974  0.73951776\n",
      " 0.5708267  0.10380456 0.29606703 0.20044971 0.5620758  0.84491645\n",
      " 0.57554549 0.66965144 0.72612006 0.9787839  0.91429158 0.94684035\n",
      " 0.20564362 0.42324928 0.17164559 0.53530738 0.20276622 0.39543033\n",
      " 0.13191932 0.64095045 0.48093475 0.82334278 0.72429889 0.28527969\n",
      " 0.02998032 0.67193194 0.31398465 0.30317467 0.1717566  0.95993788\n",
      " 0.72701651 0.13955576 0.84745719 0.6134221  0.77751058 0.48198063\n",
      " 0.29694771 0.44984781 0.78802719 0.59219795 0.82273827 0.4237234\n",
      " 0.06312366 0.34707282 0.84547402 0.20421981 0.05193159 0.1363337\n",
      " 0.37998095 0.11027999 0.10025673 0.29473218]\n"
     ]
    }
   ],
   "source": [
    "# 代码 2-14\n",
    "print('生成的随机数组为：\\n', np.random.random(100))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "063d9dff",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "生成的随机数组为：\n",
      " [[0.53225034 0.21361476 0.32890683 0.52571017 0.33064003]\n",
      " [0.22421507 0.25929834 0.16496774 0.16165658 0.848496  ]\n",
      " [0.00300543 0.96602315 0.34993224 0.94807282 0.41506844]\n",
      " [0.01452865 0.42603049 0.02959856 0.14133574 0.59735863]\n",
      " [0.68701335 0.48089042 0.7411118  0.99479835 0.72059834]\n",
      " [0.11233222 0.29556017 0.99300937 0.95159343 0.31678385]\n",
      " [0.81304559 0.0215905  0.13777947 0.96117381 0.39227752]\n",
      " [0.23917912 0.91368055 0.60150666 0.96031561 0.83718895]\n",
      " [0.2040396  0.70671295 0.43732317 0.46283076 0.35788911]\n",
      " [0.76771457 0.44576607 0.0781334  0.90289581 0.05080656]]\n"
     ]
    }
   ],
   "source": [
    "# 代码 2-15\n",
    "print('生成的随机数组为：\\n', np.random.rand(10,5))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "21fb5596",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "生成的随机数组为：\n",
      " [[-0.17412267 -1.33325909  1.07616806]\n",
      " [ 0.12270305 -1.33568725 -1.69824849]]\n"
     ]
    }
   ],
   "source": [
    "# 代码 2-16\n",
    "print('生成的随机数组为：\\n', np.random.randn(2,3))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "e2203e47",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "生成的随机数组为：\n",
      " [[6 4 7 8 8]\n",
      " [8 3 8 6 7]]\n"
     ]
    }
   ],
   "source": [
    "# 代码 2-17\n",
    "np.random.seed(666)\n",
    "print('生成的随机数组为：\\n', np.random.randint(2, 10, size=[2,5]))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "48ef57be",
   "metadata": {},
   "source": [
    "# 2.1.3 通过索引访问数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "id": "1f0c49dd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 代码 2-18\n",
    "arr = np.arange(10)\n",
    "arr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f12f2a23",
   "metadata": {},
   "outputs": [],
   "source": [
    "print('索引结果为：', arr[5])  # 用整数作为下标可以获取数组中的某个元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "id": "b5ecf587",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "索引结果为： [3 4]\n"
     ]
    }
   ],
   "source": [
    "# 用范围作为下标获取数组的一个切片，包括arr[3]不包括arr[5]\n",
    "print('索引结果为：', arr[3:5])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "id": "32689efe",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "索引结果为： [0 1 2 3 4]\n"
     ]
    }
   ],
   "source": [
    "print('索引结果为：', arr[:5])  # 省略开始下标，表示从arr[0]开始"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "id": "408d76ab",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "索引结果为： 9\n"
     ]
    }
   ],
   "source": [
    "# 下标可以使用负数，-1表示从数组后往前数的第一个元素\n",
    "print('索引结果为：', arr[-1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "id": "a649b665",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "创建的二维数组为：\n",
      " [[ 1  2  3  4  5]\n",
      " [ 4  5  6  7  8]\n",
      " [ 7  8  9 10 11]]\n"
     ]
    }
   ],
   "source": [
    "# 代码 2-19\n",
    "arr = np.array([[1, 2, 3, 4, 5],[4, 5, 6, 7, 8], [7, 8, 9, 10, 11]])\n",
    "print('创建的二维数组为：\\n', arr)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "45db5151",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "id": "7c5bd20b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "索引结果为： [4 5]\n"
     ]
    }
   ],
   "source": [
    "print('索引结果为：', arr[0,3:5])  # 索引第0行中第4和第5列的元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "id": "0df5ae6d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "索引结果为：\n",
      " [[ 6  7  8]\n",
      " [ 9 10 11]]\n"
     ]
    }
   ],
   "source": [
    "# 索引第2和第3行中第3列、第4列和第5列的元素\n",
    "print('索引结果为：\\n', arr[1:,2:])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "id": "31fea197",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "索引结果为： [3 6 9]\n"
     ]
    }
   ],
   "source": [
    "print('索引结果为：', arr[:,2])  # 索引第3列的元素"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "12a78292",
   "metadata": {},
   "source": [
    "# 2.1.4变换数组形状"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e52edab2",
   "metadata": {
    "heading_collapsed": true
   },
   "source": [
    "## 展平"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "id": "e243a087",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "创建的一维数组为： [ 0  1  2  3  4  5  6  7  8  9 10 11]\n"
     ]
    }
   ],
   "source": [
    "# 代码 2-21\n",
    "arr = np.arange(12)  # 创建一维数组\n",
    "print('创建的一维数组为：', arr)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "id": "ac61a44d",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "新的一维数组为：\n",
      " [[ 0  1  2  3]\n",
      " [ 4  5  6  7]\n",
      " [ 8  9 10 11]]\n"
     ]
    }
   ],
   "source": [
    "print('新的一维数组为：\\n', arr.reshape(3, 4))  # 设置数组的形状"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "id": "62538ca2",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "数组维度为： 2\n"
     ]
    }
   ],
   "source": [
    "print('数组维度为：', arr.reshape(3, 4).ndim)  # 查看数组维度"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "id": "a5fcbfcb",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "创建的二维数组为：\n",
      " [[ 0  1  2  3]\n",
      " [ 4  5  6  7]\n",
      " [ 8  9 10 11]]\n",
      "数组展平后为： [ 0  1  2  3  4  5  6  7  8  9 10 11]\n"
     ]
    }
   ],
   "source": [
    "# 代码 2-22\n",
    "arr = np.arange(12).reshape(3, 4)\n",
    "print('创建的二维数组为：\\n', arr)\n",
    "print('数组展平后为：', arr.ravel())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "id": "05f93726",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "数组展平为： [ 0  1  2  3  4  5  6  7  8  9 10 11]\n"
     ]
    }
   ],
   "source": [
    "# 代码 2-23\n",
    "print('数组展平为：', arr.flatten())  # 横向展平"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "id": "456429ec",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "数组展平为： [ 0  4  8  1  5  9  2  6 10  3  7 11]\n"
     ]
    }
   ],
   "source": [
    "print('数组展平为：', arr.flatten('F'))  # 纵向展平"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "85339fc0",
   "metadata": {
    "heading_collapsed": true
   },
   "source": [
    "## 组合"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "id": "648350fd",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "创建的数组1为：\n",
      " [[ 0  1  2  3]\n",
      " [ 4  5  6  7]\n",
      " [ 8  9 10 11]]\n"
     ]
    }
   ],
   "source": [
    "# 代码 2-24\n",
    "arr1 = np.arange(12).reshape(3, 4)\n",
    "print('创建的数组1为：\\n', arr1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "id": "69babf96",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "创建的数组2为：\n",
      " [[ 0  3  6  9]\n",
      " [12 15 18 21]\n",
      " [24 27 30 33]]\n"
     ]
    }
   ],
   "source": [
    "arr2 = arr1*3\n",
    "print('创建的数组2为：\\n', arr2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "id": "5cc84e52",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "横向组合为：\n",
      " [[ 0  1  2  3  0  3  6  9]\n",
      " [ 4  5  6  7 12 15 18 21]\n",
      " [ 8  9 10 11 24 27 30 33]]\n"
     ]
    }
   ],
   "source": [
    "print('横向组合为：\\n', np.hstack((arr1, arr2)))  # hstack函数横向组合"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "id": "131a42c1",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "纵向组合为：\n",
      " [[ 0  1  2  3]\n",
      " [ 4  5  6  7]\n",
      " [ 8  9 10 11]\n",
      " [ 0  3  6  9]\n",
      " [12 15 18 21]\n",
      " [24 27 30 33]]\n"
     ]
    }
   ],
   "source": [
    "# 代码 2-25\n",
    "print('纵向组合为：\\n', np.vstack((arr1, arr2)))  # vstack函数纵向组合"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "id": "117166d7",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "纵向组合为：\n",
      " [[ 0  3  6  9]\n",
      " [12 15 18 21]\n",
      " [24 27 30 33]\n",
      " [ 0  1  2  3]\n",
      " [ 4  5  6  7]\n",
      " [ 8  9 10 11]]\n"
     ]
    }
   ],
   "source": [
    "# 代码 2-25\n",
    "print('纵向组合为：\\n', np.vstack((arr2, arr1)))  # vstack函数纵向组合"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "42301f0c",
   "metadata": {
    "hidden": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "id": "d86f396f",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "横向组合为：\n",
      " [[ 0  1  2  3  0  3  6  9]\n",
      " [ 4  5  6  7 12 15 18 21]\n",
      " [ 8  9 10 11 24 27 30 33]]\n"
     ]
    }
   ],
   "source": [
    "# 代码 2-26\n",
    "print('横向组合为：\\n', np.concatenate((arr1, arr2), axis=1))  # concatenate函数横向组合"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "id": "d2d42f4e",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "纵向组合为：\n",
      " [[ 0  1  2  3]\n",
      " [ 4  5  6  7]\n",
      " [ 8  9 10 11]\n",
      " [ 0  3  6  9]\n",
      " [12 15 18 21]\n",
      " [24 27 30 33]]\n"
     ]
    }
   ],
   "source": [
    "print('纵向组合为：\\n', np.concatenate((arr1, arr2), axis=0))  # concatenate函数纵向组合"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5b107fbc",
   "metadata": {
    "heading_collapsed": true
   },
   "source": [
    "## 分割"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "id": "8a941b24",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "创建的二维数组为：\n",
      " [[ 0  1  2  3]\n",
      " [ 4  5  6  7]\n",
      " [ 8  9 10 11]\n",
      " [12 13 14 15]]\n"
     ]
    }
   ],
   "source": [
    "# 代码 2-27\n",
    "arr = np.arange(16).reshape(4, 4)\n",
    "print('创建的二维数组为：\\n', arr)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "id": "d2ec44e0",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "横向分割为：\n",
      " [array([[ 0,  1],\n",
      "       [ 4,  5],\n",
      "       [ 8,  9],\n",
      "       [12, 13]]), array([[ 2,  3],\n",
      "       [ 6,  7],\n",
      "       [10, 11],\n",
      "       [14, 15]])]\n"
     ]
    }
   ],
   "source": [
    "print('横向分割为：\\n', np.hsplit(arr, 2))  # hsplit函数横向分割"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "id": "74c34562",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "纵向分割为：\n",
      " [array([[0, 1, 2, 3],\n",
      "       [4, 5, 6, 7]]), array([[ 8,  9, 10, 11],\n",
      "       [12, 13, 14, 15]])]\n"
     ]
    }
   ],
   "source": [
    "# 代码 2-28\n",
    "print('纵向分割为：\\n', np.vsplit(arr, 2))  # vsplit函数纵向分割"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c1f82cff",
   "metadata": {
    "hidden": true
   },
   "outputs": [],
   "source": [
    "# 代码 2-29\n",
    "print('横向分割为：\\n', np.split(arr, 2, axis=1))  # split函数横向分割"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "id": "a6c8aca5",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "纵向分割为：\n",
      " [array([[0, 1, 2, 3],\n",
      "       [4, 5, 6, 7]]), array([[ 8,  9, 10, 11],\n",
      "       [12, 13, 14, 15]])]\n"
     ]
    }
   ],
   "source": [
    "print('纵向分割为：\\n', np.split(arr, 2, axis=0))  # split函数纵向分割"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "43cee9e5",
   "metadata": {
    "hidden": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.4"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": false,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
